import java.sql.*;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        //全查询
        //m1();

        //条件查询
        //m2();

        //增加
        //m3();

        //删除
        //m4();

        //修改
        m5();

    }

    //全查询
    private static void m1() {
        Connection conn = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            //1.加载驱动
            Class.forName("com.mysql.jdbc.Driver");

            //2.获取链接
            conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jdbc?serverTimezone=GMT%2B8", "root", "123456");

            //3.编写sql语句
            String sql = "select * from sys_user";

            //4.预处理sql  .var自动生成等号左边代码
            preparedStatement = conn.prepareStatement(sql);

            //5.如果有参数，设置参数

            //6.执行sql语句
            resultSet = preparedStatement.executeQuery();

            //7.如果是查询，就遍历结果/如果是增加删除和修改，判断是否成功
            while (resultSet.next()) {
                int id = resultSet.getInt(1);
                String name = resultSet.getString(2);
                String pass = resultSet.getString(3);
                System.out.println(id + "---" + name + "---" + pass);
            }

            //8.返回视图
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //8.返回视图（关闭资源）
            try {
                if (resultSet != null){
                    resultSet.close();
                }
                if (resultSet != null){
                    preparedStatement.close();
                }
                if (resultSet != null){
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    //条件查询
    private static void m2() {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            //输入 scanner
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入要查询的姓名：");
            String word = scanner.next();

            //1.加载驱动
            Class.forName("com.mysql.jdbc.Driver");

            //2.获取链接(url,username,password)
            String url = "jdbc:mysql://127.0.0.1:3306/jdbc?serverTimezone=GMT%2B8";
            connection = DriverManager.getConnection(url, "root", "123456");

            //3.编写sql语句
            String sql = "select * from sys_user where name like ?";

            //4.预处理sql  .var自动生成等号左边代码
            preparedStatement = connection.prepareStatement(sql);

            //5.如果有参数，设置参数
            preparedStatement.setObject(1, "%" + word + "%");

            //6.执行sql语句
            resultSet = preparedStatement.executeQuery();

            //7.如果是查询，就遍历结果/如果是增加删除和修改，判断是否成功
            //遍历输出结果
            while (resultSet.next()) {
                int id = resultSet.getInt(1);
                String name = resultSet.getString(2);
                String pass = resultSet.getString(3);
                System.out.println(id + "---" + name + "---" + pass);
            }

            //8.返回视图（关闭资源）
            resultSet.close();
            preparedStatement.close();
            connection.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //8.返回视图（关闭资源）
            try {
                if (resultSet != null){
                    resultSet.close();
                }
                if (resultSet != null){
                    preparedStatement.close();
                }
                if (resultSet != null){
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    //增加
    private static void m3() {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            //输入 scanner
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入要增加的姓名：");
            String name = scanner.next();
            System.out.println("请输入要增加的密码：");
            String pass = scanner.next();

            //1.加载驱动
            Class.forName("com.mysql.jdbc.Driver");

            //2.获取链接(url,username,password)
            String url = "jdbc:mysql://127.0.0.1:3306/jdbc?serverTimezone=GMT%2B8";
            connection = DriverManager.getConnection(url, "root", "123456");
//            if (connection != null){
//                System.out.println("连接成功");
//            }

            //3.编写sql语句
            String sql = "insert into sys_user(name,pass) value (?,?) ";

            //4.预处理sql  .var自动生成等号左边代码
            preparedStatement = connection.prepareStatement(sql);

            //5.如果有参数，设置参数
            preparedStatement.setObject(1, name);
            preparedStatement.setObject(2, pass);

            //6.执行sql语句
            int result = preparedStatement.executeUpdate();

            //7.如果是查询，就遍历结果/如果是增加删除和修改，判断是否成功
            //结果判断
            if (result > 0) {
                System.out.println("增加成功。");
            }

            //8.返回视图
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //8.返回视图（关闭资源）
            try {
                if (resultSet != null){
                    resultSet.close();
                }
                if (resultSet != null){
                    preparedStatement.close();
                }
                if (resultSet != null){
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    //删除
    private static void m4() {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            //输入 scanner
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入要删除的id：");
            String id = scanner.next();

            //1.加载驱动
            Class.forName("com.mysql.jdbc.Driver");

            //2.获取链接(url,username,password)
            String url = "jdbc:mysql://127.0.0.1:3306/jdbc?serverTimezone=GMT%2B8";
            connection = DriverManager.getConnection(url, "root", "123456");
//            if (connection != null){
//                System.out.println("连接成功");
//            }

            //3.编写sql语句
            String sql = "delete from sys_user where id = ? ";

            //4.预处理sql  .var自动生成等号左边代码
            preparedStatement = connection.prepareStatement(sql);

            //5.如果有参数，设置参数
            preparedStatement.setObject(1, id);

            //6.执行sql语句
            int result = preparedStatement.executeUpdate();

            //7.如果是查询，就遍历结果/如果是增加删除和修改，判断是否成功
            //结果判断
            if (result > 0) {
                System.out.println("删除成功。");
            }

            //8.返回视图
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //8.返回视图（关闭资源）
            try {
                if (resultSet != null){
                    resultSet.close();
                }
                if (resultSet != null){
                    preparedStatement.close();
                }
                if (resultSet != null){
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    //修改
    private static void m5() {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            //输入 scanner
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入要更改的id：");
            String id = scanner.next();
//            System.out.println("请选择要更改的内容：1.名字 2.密码");
//            int nextInt = scanner.nextInt();
//            System.out.println("请输入要增加的姓名：");
//            String name = scanner.next();
//            System.out.println("请输入要增加的密码：");
//            String pass = scanner.next();

            //1.加载驱动
            Class.forName("com.mysql.jdbc.Driver");

            //2.获取链接(url,username,password)
            String url = "jdbc:mysql://127.0.0.1:3306/jdbc?serverTimezone=GMT%2B8";
            connection = DriverManager.getConnection(url, "root", "123456");
//            if (connection != null){
//                System.out.println("连接成功");
//            }

            //3.编写sql语句
            String sql = "update sys_user set name = 'cloud' where id = ? ";

            //4.预处理sql  .var自动生成等号左边代码
            preparedStatement = connection.prepareStatement(sql);

            //5.如果有参数，设置参数
            preparedStatement.setObject(1, id);

            //6.执行sql语句
            int result = preparedStatement.executeUpdate();

            //7.如果是查询，就遍历结果/如果是增加删除和修改，判断是否成功
            //结果判断
            if (result > 0) {
                System.out.println("更改成功。");
            }

            //8.返回视图（关闭资源）
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //8.返回视图（关闭资源）
            try {
                if (resultSet != null){
                    resultSet.close();
                }
                if (resultSet != null){
                    preparedStatement.close();
                }
                if (resultSet != null){
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
